package chapter1.XML.superMarket;


import java.io.Serializable;

public class Supermarket implements Serializable {
    private String name;
    private String address;
    private Member[] people = new Member[20];
    private int count = 0;//会员人数

    public Supermarket() {
    }

    public Supermarket(String name, String address) {
        this.name = name;
        this.address = address;
    }

    //显示超市和会员信息
    public void show() {
        System.out.println("超市的名字是:" + name + ",地址是:" + address);
        System.out.println("超市会员信息为:");
        for (int i = 0; i < count; i++) {
            people[i].show();
        }
    }

   
    public boolean add(Member newMember) {

     
        if (chechCardNoRepeat(newMember.getCardNo())) {    
          
            System.out.println("会员号重复，不能添加");
            return false;
        }

        if (chechPhoneRepeat(newMember.getPhone())) {      
            //新会员的手机号重复，不能添加
            System.out.println("手机号重复，不能添加");
            return false;
        }

        if (chechJifenMax(newMember.getJifen())) {
            //新会员的积分超过600，不能添加
            System.out.println("积分超过600，不能添加");
            return false;
        }

        //给数组扩容
        if (count >= people.length) {   
            Member[] people2 = new Member[people.length * 2];
            for (int i = 0; i < people.length; i++) {
                people2[i] = people[i];
            }
            people = people2;
        }

        people[count++] = newMember;
        return true;
    }

    //判断新会员的会员号号是否重复
    public boolean chechCardNoRepeat(String cardNo) {
        for (int i = 0; i < count; i++) {
            if (people[i].getCardNo().equals(cardNo))
                return true;
        }
        return false;
    }

    //判断新会员的手机号是否重复
    public boolean chechPhoneRepeat(String phone) {
        for (int i = 0; i < count; i++) {
            if (people[i].getPhone().equals(phone))
                return true;
        }
        return false;
    }

    //限制新会员的积分不能超过600
    public boolean chechJifenMax(int jifen) {
        if (jifen > 600) {
            return true;
        }
        return false;
    }

    //给指定会员号的用户涨分
    public boolean inc(String cardNo, int fen) {
        for (int i = 0; i < count; i++) {
            if (people[i].getCardNo().equals(cardNo)) {
                people[i].setJifen(people[i].getJifen() + fen);
                return true;
            }
        }
        return false;
    }

    //兑换礼品
    public String duiHuan(String cardNo, int lipingleixing) {
        for (int i = 0; i < count; i++) {
            if (people[i].getCardNo().equals(cardNo)) {
                if (lipingleixing == 1) {
                    if (people[i].getJifen() >= 100) {
                        people[i].setJifen(people[i].getJifen() - 100);
                        return "餐巾纸一包";
                    }
                } else if (lipingleixing == 2) {
                    if (people[i].getJifen() >= 400) {
                        people[i].setJifen(people[i].getJifen() - 400);
                        return "剃须刀一把";
                    }
                } else if (lipingleixing == 3) {
                    if (people[i].getJifen() >= 1000) {
                        people[i].setJifen(people[i].getJifen() - 1000);
                        return "炒锅一个";
                    }
                }
            }
        }
        return null;    //其它所有的情况，返回null，表示兑换失败
    }

    //获得超市会员人数
    public int getRenShu() {
        return count;
    }


    //获得最大积分的会员
    public Member getBiggestJifen() {  
        if (count == 0)
            return null;

        Member maxJiFenPeople = people[0];
        for (int i = 1; i < count; i++) {
            if (people[i].getJifen() > maxJiFenPeople.getJifen())
                maxJiFenPeople = people[i];
        }
        return maxJiFenPeople;
    }


    //查询会员积分
    public int getJiFen(String cardNo) {
        if (count == 0)
            return -1;
        for (int i = 0; i < count; i++) {
            if (cardNo.equals(people[i].getCardNo()))
                return people[i].getJifen();
        }
        return -1;  //根据会员号，没找到会员
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Member[] getPeople() {
        return people;
    }

    public void setPeople(Member[] people) {
        this.people = people;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
